﻿var business = business ||
    {
        getBusinessInfoes: window.prefixURL + "Business/GetBusinessInfoes",
        getBusinessInfo: window.prefixURL + "Business/GetBusinessInfo",
        delBusinessInfo: window.prefixURL + "Business/DeleteBusinessInfo",
        addOrUpdateBusinessInfo: window.prefixURL + "Business/AddOrUpdateBusinessInfo",

        bodyTpl: _.template($("#listTemplate").html()),
        pagerTpl: _.template($("#pagingTablePager").html()),

        pageIndex: function () {
            return $('#nav').find('li[flag]').find('a').html();
        },

        pageSize: function () {
            return $('#nav').find('.pagingTablePageCountSelect').val();
        },

        getList: function (pageIndex) {
            var t = this;
            var url = t.getBusinessInfoes + "?pageIndex=" + pageIndex + "&pageSize=10";
            $.ajax({
                url: url,
                type: "GET",
                dataType: "Json",
                success: function (data) {
                    if (data != null) {
                        $("#tbody").html(t.bodyTpl(data.returnObj));
                        $("#nav").html(t.pagerTpl(data.pager));
                        $(".pagingTablePageCountSelect").val(data.pager.PageSize);
                    }
                },
                error: function (data) {

                }
            });
        },

        del: function (sender, id) {
            var returnVal = confirm("Are you sure？");
            if (returnVal === false) {
                return;
            } else {
                $.ajax({
                    url: sender.delBusinessInfo + "?id=" + id,
                    type: "GET",
                    dataType: "Json",
                    success: function (data) {
                        if (data.returnObj === true) {
                            alert("Success！");
                            sender.getList(1);
                        } else {
                            alert("Error:" + data.businessMessage);
                        }
                    }
                });
            }
        },

        edit: function (sender, id) {
            $.ajax({
                url: sender.getBusinessInfo + "?id=" + id,
                type: "GET",
                dataType: "Json",
                success: function (data) {
                    if (data.businessMessage !== -1) {
                        $("#txtBusinessId").val(data.returnObj.id);
                        $("#txtName").val(data.returnObj.name);
                        $("#txtDecription").val(data.returnObj.description);
                        $("#txtPIC").val(data.returnObj.picName);
                        $("#selectEnvs").val(data.returnObj.environmentId)
                        $("#createModel").modal();
                    } else {
                        alert(data.businessMessage);
                    }
                },
                error: function (data) {
                    alert("Error:" + data);
                }
            });
        },

        initPage: function () {
            var t = this;
            t.getList(1);
        },

        bindEvents: function () {
            var t = this;

            $("span[name='opt-create']").bind("click", function () {
                //$("#").val("");
                $("#txtName").val("");
                $("#txtDecription").val("");
                $("#selectEnvs").val("");
                $("#txtPIC").val("");

                $("#crtHeader").html("Create Business");
                $("#createModel").modal();
            });

            $("#tbody").delegate("tr td span", "click", function () {
                var type = $(this).data("type");
                var id = $(this).data("id");
                if (type === "edit") {
                    t.edit(t, id);
                } else {
                    t.del(t, id);
                }
            });

            $("#btnSubmit").bind("click", function () {

                var $btn = $(this).button("loading...");
                var id = $("#txtBusinessId").val();
                var name = $("#txtName").val();
                var description = $("#txtDecription").val();
                var env = $("#selectEnvs").val();
                var pic = $("#txtPIC").val();

                $.ajax({
                    url: t.addOrUpdateBusinessInfo,
                    type: "POST",
                    dataType: "Json",
                    data: {
                        Id: id,
                        Name: name,
                        Description: description,
                        EnvironmentId: env,
                        PICName: pic
                    },
                    success: function (data) {
                        if (data.returnObj === true) {
                            $("#createModel").modal('hide');
                            $btn.button('reset');
                            t.getList(1);
                        } else {
                            $("#createErrorMsg").show().children("div").find("span").html(data.businessMessage);
                            $btn.button('reset');
                        }
                    },
                    error: function (event) {
                        alert("Error:" + event);
                    }
                });
            });

            $("#nav").delegate("ul li:not('.disabled')", 'click', function () {
                var arialabel = $(this).children('a').attr('aria-label');
                var pageIndex = $(this).children('a').attr('data-page');
                var activePageIndex = t.pageIndex();
                if (arialabel != undefined) {
                    if (arialabel === 'Previous') {
                        t.getList(parseInt(activePageIndex) - 1);
                    } else if (arialabel === 'Next') {
                        t.getList(parseInt(activePageIndex) + 1);
                    }
                } else {
                    t.getList(pageIndex);
                }
            });
        },

        //@入口方法
        init: function () {
            var t = this;
            t.bindEvents();
            t.initPage();
        }
    }.init();